<?php
/**
 *
 * @package    gadgets
 * @author     Daniel Botelho <botelho.daniel@gmail.com>
 */
class GadgetsUtil
{
  const DEFAULT_CLASS = 'gadgets.util';
   
  /**
   * Escapes the input using html entities to make it safer.
   */
  public static function escapeString($text,$is_javascript=false,$appendable=false)
  {
    if(!$is_javascript)
      $text ="'$text'";
  	return StringUtil::appendableString("gadgets.util.escapeString($text)",$appendable);
  }
  
  public static function supportsEscapeString($text=null,$js_text=false,$else=null,$js_else=false,$appendable=true)
  {
    if(!$text)
      return StringUtil::appendableString("typeof gadgets.util.escapeString == 'function'",true);
    
    if($else && $js_else )
      return StringUtil::appendableString("(typeof gadgets.util.escapeString == 'function')? ".GadgetsUtil::escapeString($text,$js_text,true).": $else",$appendable);
    else
      return StringUtil::appendableString("(typeof gadgets.util.escapeString == 'function')? ".GadgetsUtil::escapeString($text,$js_text,true).": '$else'",$appendable);
  }
    
  /**
   * Gets the feature parameters.
   */
  public static function getFeatureParameters($feature,$appendable=false)
  {
  	return StringUtil::appendableString("gadgets.util.getFeatureParameters($feature)",$appendable);
  }
  
  /**
   * Gets the URL parameters.
   */
  public static function getUrlParameters($appendable=false)
  {
  	return StringUtil::appendableString("gadgets.util.getUrlParameters()",$appendable);
  }
  
  /**
   * Returns whether the current feature is supported
   */
  public static function hasFeature($feature,$appendable=false)
  {
  	return StringUtil::appendableString("gadgets.util.hasFeature($feature)",$appendable);
  }
  
  /**
   * Reverses escapeString.
   */
  public static function unescapeString($str,$is_javascript=false,$appendable=false)
  {
    if(!$is_javascript)
      $str ="'$str'";
  	return StringUtil::appendableString("gadgets.util.unescapeString($str)",$appendable);
  }
  
  /**
   * Registers an onload handler.
   */
  public static function registerOnLoadHandler(JSFunction $callback,$appendable=false)
  {
  	return StringUtil::appendableString("gadgets.util.registerOnLoadHandler(".$callback->getName().")",$appendable);
  }
  
  /**
   * Creates a closure that is suitable for passing as a callback. Any number of arguments may be passed to the callback; they will be received in the order they are passed in.
   */
  public static function makeClosure($scope, $callback, $var_args,$appendable=false)
  {
  	//FIXME: não implementado
  	throw new Exception("Not yet implemented");
  }
    
}